<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      #parent {
        /* display: flex;
        justify-content: center;
        align-items: center; */
        width: 200px;
        height: 200px;
        border: 1px solid red;
      }

      #child {
        width: 80px;
        height: 80px;
        border: 1px solid green;
      }

      p {
        width: 50px;
        height: 30px;
        background-color: skyblue;
      }
      h1 {
        background-color: pink;
      }
    </style>
  </head>
  <body>
    <div id="parent">
      <div id="child"></div>
      <p>苹果</p>
      <h1>香蕉</h1>
    </div>
    <script>
      var parentEle = document.getElementById('parent');
      var childEle = document.getElementById('child');
      parentEle.addEventListener('click', function (event) {
        // console.log('父亲单击了');
        // 获取事件发生主体的标签名称
        var tagName = event.target.localName;
        if (tagName === 'p') {
          console.log('苹果真好吃');
        } else if (tagName === 'h1') {
          console.log('香蕉也不错');
        }
      });

      // 利用事件冒泡机制在配合事件对象的target可以实现事件代理（委托）。好处：可以提高事件的绑定性能。

      childEle.addEventListener('click', function () {
        console.log('儿子单击了');
      });
    </script>
  </body>
</html>
